import Vue from 'vue';
import 'vant/lib/index.css'
import { Image as VanImage,Form,Button,Field,Toast } from 'vant';
Vue.use(VanImage);
Vue.use(Form);
Vue.use(Button);
Vue.use(Field);
Vue.use(Toast);
require('../../../static/css/user/login/style.css');
export default {
    data() {
        return {
            sendCodeButtonContent:'发送验证码',
            phone: '',
            code: '',
        }
    },
    mounted() {

    },
    methods: {
        onSubmit(values) {
            var _this = this;
            if(!(/^1\d{10}$/.test(_this.phone))){
                Toast('请输入正确的手机号');
                return false; 
            }
            if(!(/\d{6}$/.test(_this.code))){
                Toast('请输入正确的验证码');
                return false; 
            }
            Toast.loading({
                duration: 0,
                message: '请求登录中...',
            });
            _this.$axios.post('/login?platform=3',{
                main_channel:1,
                t:_this.$get_time(),
                app_version:'1.0',
                phone:_this.phone,
                code:_this.code,
                type:1
            })
            .then((res)=>{
                //手动关闭上面的Loading
                Toast.clear();
                if(res.code == 200) {
                    Toast.success(res.data.msg);
                    // 基础参数写入缓存
                    sessionStorage.setItem("UserInfo", {user_id:res.data.user_id,is_new:res.data.is_new});
                    // 跳转到首页
                    window.location.href = '/';
                } else {
                    Toast.fail(res.msg);
                }
            })
        },
        sendCode(){
            var _this = this;
            if(!(/^1\d{10}$/.test(_this.phone))){
                Toast('请输入正确的手机号');
                return false; 
            }
            if (_this.sendCodeButtonContent == '发送验证码') {
                Toast.loading({
                    duration: 0,
                    message: '请求发送验证码中...',
                });
                _this.$axios.post('/send.code?platform=3',{
                    main_channel:1,
                    t:_this.$get_time(),
                    app_version:'1.0',
                    phone:_this.phone,
                    type:1
                })
                .then((res)=>{
                    //手动关闭上面的Loading
                    Toast.clear();
                    if(res.code == 200) {
                        Toast.success(res.msg);
                        var time = 60,
                        t = setInterval(()=>{
                            if(time>0) {
                                time -- ;
                                _this.sendCodeButtonContent = time+'s'
                            } else {
                                _this.sendCodeButtonContent = '发送验证码'
                                clearInterval(t);
                            }
                        },1000)
                    } else {
                        Toast.fail(res.msg);
                    }
                })
                
            }else{
                Toast(_this.sendCodeButtonContent.replace("s","秒") + '可以重新获取验证码');
                return false; 
            }
        }
    }
}